<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="区域">
							<uni-data-select
								v-model="searchModel.regionCode"
								placeholder="请选择区域"
								:localdata="setStore.allProvinceList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="限时券名称">
							<uni-easyinput v-model="searchModel.name" placeholder="请输入限时券名称" />
						</uni-forms-item>
						<uni-forms-item label="商品名称">
							<uni-easyinput v-model="searchModel.productName" placeholder="请输入商品名称" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
		</up-popup>
		<view class="content">
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					:isOperateBtn="(row) => {
						return checkPermi(['zhouyz:couponProductRecord:list', 'zhouyz:couponProductType7:remove']) ||
						(checkPermi(['zhouyz:couponProductType7:stop']) && row.status === 0)
					}"
					stripe
					border>
					<template v-slot:coupon="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">名称：<text class="table-td-text">{{row.name}}</text></view>
							<view class="table-td-row">区域：<uni-tag size="small" :text="arrText(setStore.allProvinceList, row.regionCode)" /></view>
							<view class="table-td-row">商品：<text class="table-td-text">{{row.productName}}</text></view>
							<view class="table-td-row">单位：<text class="table-td-text">{{row.productUnit}}</text></view>
							<view class="table-td-row">状态：<uni-tag size="small" :type="row.status === 1 ? 'error' : 'success'" :text="arrText(couponStatusList, row.status)" /></view>
							<view class="table-td-row">优惠金额：<text class="table-td-text red">{{row.discountPrice}}</text></view>
						</view>
					</template>
					<template v-slot:time="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">标题：<text class="table-td-text">{{row.ad}}</text></view>
							<view class="table-td-row main-flex">
								背景页：
								<view class="table-td-row-img" @click.stop="previewImg(row.adImg)">
									<up-image width="80rpx" bgColor="#ffffff" height="80rpx" :src="row.adImg" :lazy-load="true"></up-image>
								</view>
							</view>
							<view class="table-td-row">备注：<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:num="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">开始：<text class="table-td-text">{{row.startTime}}</text></view>
							<view class="table-td-row">结束：<text class="table-td-text">{{row.endTime}}</text></view>
							<view class="table-td-row">有效期(h)：<text class="table-td-text">{{row.expirationHour}}</text></view>
							<view class="table-td-row">券总量：<text class="table-td-text">{{row.totalNumber}}</text></view>
							<view class="table-td-row">已领取：<text class="table-td-text">{{row.receiveNumber}}</text></view>
							<view class="table-td-row">已过期：<text class="table-td-text">{{row.overdueNumber}}</text></view>
						</view>
					</template>
					<template v-slot:operate="{ row }">
						<view class="table-td-operate table-cell table-td-row main-flex">
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:couponProductRecord:list'])">
								<up-button
									size="mini"
									type="primary"
									@click.stop="couponProductRecordFun(row)">
									<uni-icons class="table-operate-btn-icon" type="list" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">领取记录</view>
								</up-button>
							</view>
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:couponProductType7:stop']) && row.status === 0">
								<up-button
									class="table-operate-btn main-flex"
									size="mini"
									type="warning"
									@click.stop="stopFun(row.id)">
									<uni-icons class="table-operate-btn-icon" type="smallcircle" size="20rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">停用</view>
								</up-button>
							</view>
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:couponProductType7:remove'])">
								<up-button
									class="table-operate-btn main-flex"
									size="mini"
									type="error"
									@click.stop="deleteFun(row.id)">
									<uni-icons class="table-operate-btn-icon" type="trash" size="20rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">删除</view>
								</up-button>
							</view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { couponProductType7, couponProductType7Stop, couponProductType7Del } from "@/api/erp_api/coupon"
import { allProvince } from "@/api/erp_api/region"
import { isObjectEmptySimple, copyText, arrText, previewImg } from '@/utils/ty.js';
import useUserStore from '@/store/modules/user';
import useSetStore from '@/store/modules/set';
import { couponStatusList } from '@/utils/config.js'

	const userStore = useUserStore()
	const setStore = useSetStore()

	// 标题设置
	const title = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		uni.setNavigationBarTitle({
			title: title.value 
		})
	})
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		name: '',
		productName: '',
		regionCode: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			name: '',
			productName: '',
			regionCode: ''
		}
		defaultParams()
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '券信息', key: 'coupon' },
		{ title: '活动信息', key: 'time' },
		{ title: '领取时间/数量', key: 'num', width: '300rpx' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value,
			del: 0,
			type: 2
		}
		if (searchModel.value.name) {
			params.name = searchModel.value.name
		}
		if (searchModel.value.productName) {
			params.productName = searchModel.value.productName
		}
		if (searchModel.value.regionCode) {
			params.regionCode = searchModel.value.regionCode
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		couponProductType7(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		uni.navigateTo({
			url: `/pages/coupon/couponProductType7Detail?id=${row.id}`
		})
	}
	// 领取记录
	const couponProductRecordFun = (row) => {
		uni.navigateTo({
			url: `/pages/coupon/couponProductRecord?couponId=${row.id}&name=${row.name}`
		})
	}
	//停用
	const stopFun = (id) => {
		uni.showModal({
			title: '停用',
			content: `确认停用优惠券信息编号为"${id}"的数据项,停用后已领用优惠券还可以使用`,
			success: function (res) {
				if (res.confirm) {
					couponProductType7Stop({
						id
					}).then(res => {
						modal.showToast('停用成功')
						getList()
					}).catch((err) => {
						modal.showToast(err)
					})
				}
			}
		})
	}
	
	//删除
	const deleteFun = (id) => {
		uni.showModal({
			title: '删除',
			content: `是否确认删除优惠券编号为"${id}"的数据项?`,
			success: function (res) {
				if (res.confirm) {
					deleteOne(id)
				}
			}
		})
	}
	const deleteOne = (id) => {
		couponProductType7Del({
			id
		}).then(res => {
			modal.showToast('删除成功')
			getList()
		}).catch((err) => {
			modal.showToast(err)
		})
	}
	const defaultParams = () => {
		searchModel.value.regionCode = userStore.regionCode
	}
	onMounted(async () => {
		defaultParams()
		searchFirst()
	})
</script>

<style scoped>
.status1 {
	color: #19be6b;
}
.status2 {
	color: #fa3534;
}
</style>
